<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录</view>
			<input class="u-border-bottom" v-model="username" placeholder="请输入用户名" />
			<input class="u-border-bottom" type="password" v-model="password" placeholder="请输入密码" password />
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">登录</button>
			<view class="alternative">
				<view class="password" @click="zc">注册</view>
				<view class="issue">遇到问题</view>
			</view>
		</view>
		<view class="buttom">
			<view class="hint">
				登录代表同意
				<text class="link">用户协议、隐私政策，</text>
				并授权使用您的账号信息（如昵称、头像、收获地址）以便您统一管理
			</view>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script setup lang="ts">
import { ref, reactive, toRefs, computed } from 'vue'
import { userLoginApi } from '../../https/http'
const data = reactive<{
	username: string,
	password: string
}>({
	username: '',
	password: ''
})
const inputStyle = computed(() => {
	let style: {
		color?: string
		backgroundColor?: string
	} = {};
	if (!data.username && !data.password) {
		style.color = "#fff";
		style.backgroundColor = "rgb(253, 243, 208)";
	}
	return style;
})
const messageToggle = (type: any) => {
	uni.showToast({
		title: type,
		icon: 'none'
	})
}
const submit = async () => {
	if (data.username && data.password) {
		let res = await userLoginApi(data)
		if (res.code == 200) {
			const nickname = (res as any).nickname 
			try {
				uni.setStorageSync('d_token', res.data.token);
				uni.setStorageSync('d_user', nickname);
				uni.setStorageSync('d_usern', res.username);
				uni.setStorageSync('d_avatar', res.data.avatar);
			} catch (e) {
				// error
				console.log(e);
			}
			uni.switchTab({
				url: '/pages/index/index'
			})
		} else {
			messageToggle(res.error)
		}
	}
}
const zc = () => {
	uni.navigateTo({
		url: '/pages/zhuc/zhuc'
	})
}
const { username, password } = toRefs(data)
</script>

<style lang="scss" scoped>
.wrap {
	font-size: 28rpx;

	.content {
		width: 600rpx;
		margin: 80rpx auto 0;

		.title {
			text-align: left;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}

		input {
			text-align: left;
			margin-bottom: 10rpx;
			padding-bottom: 6rpx;
		}

		.tips {
			margin-bottom: 60rpx;
			margin-top: 8rpx;
		}

		.getCaptcha {
			background-color: rgb(253, 243, 208);
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;

			&::after {
				border: none;
			}
		}

		.alternative {
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
		}
	}

	.buttom {
		.loginType {
			display: flex;
			padding: 350rpx 150rpx 150rpx 150rpx;
			justify-content: space-between;

			.item {
				display: flex;
				flex-direction: column;
				align-items: center;
				font-size: 28rpx;
			}
		}

		.hint {
			padding: 20rpx 40rpx;
			font-size: 20rpx;

			.link {
				color: rgb(66, 66, 252);
			}
		}
	}
}
</style>
